<script setup lang="ts">
  import { useUpdateWarrantyDateRangeCheck } from '@/services/pages/code/useCode'
  import { watch } from 'vue'
  import { HMessage, HMessageBox } from '@/components/common/message'

  const { checkForm, checkLoading, check, resetForm } = useUpdateWarrantyDateRangeCheck()

  defineOptions({
    name: 'UpdateWarrantyDateRangeCheckModal',
  })

  const props = defineProps({
    updateWarrantyDateRangeLogRowId: {
      type: String,
      required: true,
    },
  })

  const emits = defineEmits<{
    (e: 'update'): void
  }>()

  // 弹窗显示状态
  const visible = defineModel('modelValue', { type: Boolean, default: false })

  /**
   * 隐藏弹窗
   */
  const hideDialog = () => {
    resetForm()
    visible.value = false
  }

  /**
   * 填写审核结果事件
   * @param {1 | 2} checkResult - 审核结果：1=审核通过，2=驳回
   */
  const handleFillCheckResult = async (checkResult: 1 | 2) => {
    checkForm.value.applyState = checkResult
    const res = await check()
    if (res.code === 20000) {
      HMessage.success(checkResult === 1 ? '通过成功' : '驳回成功')
      hideDialog()
      emits('update')
      return
    }
    HMessageBox.error(res.message)
  }

  watch(visible, (value) => {
    if (value) {
      checkForm.value.id = props.updateWarrantyDateRangeLogRowId
    }
  })
</script>

<template>
  <h-dialog v-model="visible" title="审核更改质保周期" :loading="checkLoading" @close="hideDialog">
    <el-form :model="checkForm" inline-message label-position="top" class="modal_form_content">
      <el-form-item label="备注">
        <el-input
          v-model="checkForm.applyMsg"
          placeholder="请输入备注"
          type="textarea"
          maxlength="200"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <h-button is-modal type="primary" @click="handleFillCheckResult(1)">通过</h-button>
      <h-plain-button is-modal type="error" @click="handleFillCheckResult(2)">驳回</h-plain-button>
    </template>
  </h-dialog>
</template>

<style scoped lang="scss">
  :deep(.el-textarea__inner) {
    height: 100px;
  }
</style>
